package org.springblade.modules.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.parts.SparepartPricesSearchDTO;
import org.springblade.modules.admin.dto.plan.PlanSearchDTO;
import org.springblade.modules.admin.dto.rescue.RescueListDTO;
import org.springblade.modules.admin.enums.MaintainPlanContentEnum;
import org.springblade.modules.admin.enums.TaskStatusEnum;
import org.springblade.modules.admin.mapper.MaintainPlanContentMapper;
import org.springblade.modules.admin.service.IBusinessApproveService;
import org.springblade.modules.admin.service.IMaintainMalfunctionRescueService;
import org.springblade.modules.admin.service.IMaintainPlanContentService;
import org.springblade.modules.admin.service.ISparepartPricesService;
import org.springblade.modules.admin.vo.*;
import org.springblade.modules.admin.vo.plan.PlanListVo;
import org.springblade.modules.admin.vo.plan.PlanResultVo;
import org.springblade.modules.admin.vo.plan.PlanSearchDTOV2;
import org.springblade.modules.admin.vo.plan.PlanUserSignDetailVo;
import org.springblade.modules.admin.vo.rescue.RescueListVo;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 离职列表Controller
 *
 * @author: Sharry
 * @createTime: 2023/3/13 14:04
 * @version: Version-1.0
 */
@Slf4j
@Api(tags = "员工离职校验列表", value = "leave/show")
@RestController
@RequestMapping("leave/show")
public class LeaveJobExcelController {

	@Autowired
	private IMaintainMalfunctionRescueService maintainMalfunctionRescueService;

	@Autowired
	private IMaintainPlanContentService maintainPlanContentService;

	@Autowired
	private IUserService userService;

	@Autowired
	private ISparepartPricesService sparepartPricesService;

	@Autowired
	private IBusinessApproveService businessApproveService;

	/**
	 * [业务解除-保养任务]——导出Excel
	 * @param userIdMap 即将解除的用户Id
	 */
	@ApiOperation("[业务解除-保养任务]——导出Excel")
	@ApiOperationSupport(order = 11)
	@PostMapping("/maintain/list/download/excel")
	public void maintainListExcel(HttpServletResponse response, @RequestBody HashMap<String,Long> userIdMap) throws IOException {
		Long userId = userIdMap.get("userId");
		log.info("接收到的userId{}",userId);
		// 获取planIds
		UserDimissionCountVO userDimissionCountVO = maintainPlanContentService.selectUserDimissionCount(userId);
		List<Long> ids = userDimissionCountVO.getChePlanIdList();
		PlanSearchDTO planSearchDTO = new PlanSearchDTO();
		planSearchDTO.setPlanIds(ids);

		//暴力条件
		planSearchDTO.setCurrent(1);
		planSearchDTO.setSize(Integer.MAX_VALUE);

		//其它条件
		List<Integer> status = new ArrayList<>();
		status.add(TaskStatusEnum.BY_DISABLE.getStatus());
		status.add(TaskStatusEnum.BY_PROCESSED.getStatus());
		status.add(TaskStatusEnum.BY_FINISH.getStatus());
		status.add(TaskStatusEnum.BY_MAINTAIN_SIGN.getStatus());
		planSearchDTO.setPlanStatus(status);

		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<List<String>> rows = new ArrayList<>();
			List<String> title = CollUtil.newArrayList
				("执行人", "实际执行人", "组别", "项目", "梯号", "电梯状态", "任务描述", "计划时间", "开始时间", "完成时间", "图片数量",
					"当前状态", "时效", "时长标识", "时长(分钟)", "评分状态", "工单评分", "计划调整状态", "调整证明", "已提交调整数量", "任务年审类型");
			rows.add(title);
			List<PlanListVo> records = maintainPlanContentService.planListNew(planSearchDTO).getRecords();
			for (PlanListVo planListVo : records) {
				List<String> row = new ArrayList<>(16);
				row.add(Convert.toStr(planListVo.getRealNames(), ""));
				final List<PlanUserSignDetailVo> realUserSignInfoList = planListVo.getRealUserSignInfoList();
				if (realUserSignInfoList != null && realUserSignInfoList.size() > 0) {
					List<String> list = realUserSignInfoList.stream().map(PlanUserSignDetailVo::getUserName).collect(Collectors.toList());
					row.add(String.join(",", list));
				} else {
					row.add("");
				}
				row.add(Convert.toStr(planListVo.getGroupName(), ""));
				row.add(Convert.toStr(planListVo.getBuildingName(), ""));
				row.add(Convert.toStr(planListVo.getElevatorNo(), ""));
				row.add(Convert.toInt(planListVo.getIsStop(), 1) == 1 ? "正常" : "停梯");
				row.add(Convert.toStr(planListVo.getPlanCheckName(), ""));
				row.add(Convert.toStr(planListVo.getPlanDate(), ""));
				row.add(Convert.toStr(planListVo.getPlanStartTime(), ""));
				row.add(Convert.toStr(planListVo.getPlanEndTime(), ""));
				row.add(Convert.toStr(planListVo.getImageCounts(), ""));
				row.add(Convert.toStr(TaskStatusEnum.getTaskName(MaintainPlanContentEnum.MAINTAIN_BY.getId(), planListVo.getPlanStatus()), ""));
				row.add(Convert.toStr(planListVo.getOverdueDayName(), ""));
				row.add(Convert.toStr(planListVo.getWorkMinuteName(), ""));
				row.add(Convert.toStr(planListVo.getWorkMinute(), "0") + "分钟");
				String scoreStatusName;
				if (planListVo.getPlanStatus() == TaskStatusEnum.BY_SIGN.getStatus() && planListVo.getScoreStatus() != 1) {
					scoreStatusName = "待评分";
				} else if (planListVo.getScoreStatus() == 1) {
					scoreStatusName = "已评分";
				} else {
					scoreStatusName = "/";
				}
				row.add(Convert.toStr(scoreStatusName, ""));
				row.add(Convert.toStr(planListVo.getScore(), ""));

				row.add(Convert.toStr(planListVo.getDeferTypeName(), ""));
				row.add(Convert.toStr(planListVo.getDeferWarrantName(), ""));
				row.add(Convert.toStr(planListVo.getDeferCounts(), "0"));
				row.add(Convert.toStr(planListVo.getAuditType() == 1 ? "非年审" : "年审", "/"));
				rows.add(row);
			}
			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			setResponseHeader(response, fileName);
			excelWriter = ExcelUtil.getBigWriter(rows.size());
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}

	}

	/**
	 * [业务解除-维修任务]——导出Excel
	 *
	 * @param response 服务器响应参数
	 * @param userIdMap   即将解除的用户Id
	 */
	@ApiOperation("[业务解除-维修任务]——导出Excel")
	@ApiOperationSupport(order = 21)
	@PostMapping("/repair/list/download/excel")
	public void repairListExcel(HttpServletResponse response,@RequestBody HashMap<String,Long> userIdMap) throws IOException {
		Long userId = userIdMap.get("userId");
		log.info("接收到的userId{}",userId);
		//获取ids
		UserDimissionCountVO userDimissionCountVO = maintainPlanContentService.selectUserDimissionCount(userId);
		List<Long> ids = userDimissionCountVO.getMalPlanIdList();
		//条件
		PlanSearchDTOV2 planSearchDTOV2 = new PlanSearchDTOV2();
		planSearchDTOV2.setPlanIds(ids);

		List<Long> userIds = new ArrayList<>();
		userIds.add(userId);
		planSearchDTOV2.setUserIds(userIds);

		//暴力条件
		planSearchDTOV2.setCurrent(1);
		planSearchDTOV2.setSize(Integer.MAX_VALUE);

		List<PlanResultVo> records = maintainPlanContentService.getV2Page(planSearchDTOV2).getRecords();

		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<List<String>> rows = new ArrayList<>();
			List<String> title = CollUtil.newArrayList
				("任务编号", "执行人", "实际执行人", "报障状态", "电梯状态", "故障原因", "任务耗时/耗时", "组别", "项目", "梯号", "报障人", "渠道来源", "图片数量", "任务类型", "任务描述", "创建时间", "开始时间", "完成时间", "审核状态", "维修状态", "配件数量");
			rows.add(title);
			for (PlanResultVo planListVo : records) {
				List<String> row = new ArrayList<>(16);
				row.add(planListVo.getId().toString());
				row.add(planListVo.getUserNames());
				row.add(Convert.toStr(planListVo.getRealUserNames(), "/"));
				row.add(planListVo.getIsStop() == 1 ? "正常" : "停梯");
				row.add(planListVo.getElevatorIsStop() == 1 ? "正常" : "停梯");
				row.add(Convert.toStr(planListVo.getProblemItemName()));
				row.add(planListVo.getWorkMinute() == null ? "/" : Convert.toStr(planListVo.getWorkMinute()));
				row.add(planListVo.getGroupName());
				row.add(planListVo.getBuildingName());
				row.add(planListVo.getElevatorAddress());
				row.add(planListVo.getFaultName());
				row.add(planListVo.getPlanFromName());
				row.add(planListVo.getImageNum().toString());
				row.add("维修");
				row.add(Convert.toStr(planListVo.getPlanCheckTypeName(), ""));
				row.add(DateUtil.format(planListVo.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				row.add(DateUtil.format(planListVo.getBeginTime(), "yyyy-MM-dd HH:mm:ss"));
				row.add(DateUtil.format(planListVo.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
				row.add(planListVo.getApproveStatusName());
				row.add(planListVo.getStatusName());
				row.add(Convert.toStr(planListVo.getOwnPlanIds(), "0"));
				rows.add(row);
			}
			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			setResponseHeader(response, fileName);
			excelWriter = ExcelUtil.getBigWriter(rows.size());
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}
	}

	/**
	 * [业务解除-配件任务]——导出Excel
	 * @param userIdMap 即将解除的用户Id
	 */
	@ApiOperation("[业务解除-配件任务]——导出Excel")
	@ApiOperationSupport(order = 31)
	@ApiImplicitParam(name = "userId", value = "离职员工用户id", example = "1460777096537227266")
	@PostMapping("/parts/list/download/excel")
	public void partsListExcel(HttpServletResponse response, @RequestBody HashMap<String,Long> userIdMap) throws IOException {
		Long userId = userIdMap.get("userId");
		// 获取planIds
		UserDimissionCountVO userDimissionCountVO = maintainPlanContentService.selectUserDimissionCount(userId);
		List<Long> ids = userDimissionCountVO.getPartPlanIdList();
		log.info("接收到的userId{}",userId);
		//设置查询信息，查询相关数据
		SparepartPricesSearchDTO sparepartPricesSearchDTO = new SparepartPricesSearchDTO();
		sparepartPricesSearchDTO.setPlanIds(ids);

		//目标结果
		List<SparepartPricesVO> records;

		//暴力条件
		sparepartPricesSearchDTO.setCurrent(1);
		sparepartPricesSearchDTO.setSize(Integer.MAX_VALUE);

		//查询结果
		records = sparepartPricesService.planPage(sparepartPricesSearchDTO).getRecords();
		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<List<String>> rows = new ArrayList<>();
			List<String> title = CollUtil.newArrayList
				("配件任务id", "项目名称", "电梯梯号", "关联任务ID", "关联任务类型", "申请人", "配件名称", "配件型号", "数量",
					"执行人", "实际执行人", "电梯状态", "申请时间", "配件进度", "审核状态");
			rows.add(title);
			for (SparepartPricesVO s : records) {
				List<String> row = new ArrayList<>(16);
				row.add(Convert.toStr(s.getPlanId()));
				row.add(Convert.toStr(s.getBuildingName()));
				row.add(Convert.toStr(s.getElevatorAddress()));
				row.add(Convert.toStr(s.getParentPlanId()));
				row.add(Convert.toStr("维修"));
				row.add(Convert.toStr(s.getCreateUserName()));
				//配件名称处理
				String partName = s.getPartName();
				String[] split = partName.split(";");
				row.add(split[split.length-1]);
				row.add(Convert.toStr(s.getPartNO()));
				row.add(Convert.toStr(s.getPartNum()));
				row.add(Convert.toStr(s.getUserNames()));
				row.add(Convert.toStr(s.getSignUsers()));
				row.add(Convert.toStr(s.getIsStop() == 1 ? "正常" : "停梯"));
				row.add(Convert.toStr(s.getCreateTime()));
				row.add(Convert.toStr(s.getStatus() == 5 ? "待开始" : "更换中"));
				row.add("审核通过");
				rows.add(row);
			}

			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			setResponseHeader(response, fileName);
			excelWriter = ExcelUtil.getBigWriter(rows.size());
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}
	}

	/**
	 * [业务解除-救援任务]——导出Excel
	 * 将上面查看详情的工单信息，查询出现有 Excel 字段，导出
	 * @param response Excel Http 请求参数
	 * @param userIdMap   用户Id
	 * @throws IOException 导出 Excel 需抛出IOException
	 */
	@ApiOperation("[业务解除-救援任务]——导出Excel")
	@ApiOperationSupport(order = 41)
	@ApiImplicitParam(name = "userId", value = "离职员工用户id", example = "1460777096537227266")
	@PostMapping("/rescue/list/download/excel")
	public void rescueListExcel(HttpServletResponse response,@RequestBody HashMap<String,Long> userIdMap) throws IOException {
		Long userId = userIdMap.get("userId");
		log.info("接收到的userId{}",userId);
		// 获取planIds
		UserDimissionCountVO userDimissionCountVO = maintainPlanContentService.selectUserDimissionCount(userId);
		List<Long> ids = userDimissionCountVO.getRescuePlanIdList();

		//设置查询条件
		RescueListDTO rescueListDTO = new RescueListDTO();

		rescueListDTO.setPlanIds(ids);

		//暴力获取全部信息
		Query query = new Query();
		query.setSize(-1);
		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<List<String>> rows = new ArrayList<>();
			List<String> title = CollUtil.newArrayList
				("任务编号", "项目名称", "电梯位置", "救援人员", "维保组", "报警人", "报警类型", "报警来源", "报警时间",
					"救援完成时间", "耗时", "报警描述", "任务指派", "救援状态");
			rows.add(title);
			List<RescueListVo> records = maintainMalfunctionRescueService.selectMaintainMalfunctionRescuePage(Condition.getPage(query), rescueListDTO).getRecords();
			for (RescueListVo rescueListVo : records) {
				List<String> row = new ArrayList<>(16);
				row.add(Convert.toStr(rescueListVo.getId()));
				row.add(Convert.toStr(rescueListVo.getBuildingName()));
				row.add(Convert.toStr(rescueListVo.getElevatorAddress(), "/"));
				row.add(Convert.toStr(rescueListVo.getMaintainName(), "/"));
				row.add(Convert.toStr(rescueListVo.getGroupName(), "/"));
				row.add(Convert.toStr(rescueListVo.getRescueName() + rescueListVo.getRescuePhone()));
				row.add(Convert.toStr(rescueListVo.getRescueTypeName()));
				row.add(Convert.toStr(rescueListVo.getRescueSourceName()));
				row.add(Convert.toStr(DateUtil.format(rescueListVo.getCreateTime(), "yyyy-MM-dd HH:mm:ss")));
				row.add(Convert.toStr(DateUtil.format(rescueListVo.getPlanEndTime(), "yyyy-MM-dd HH:mm:ss"), "/"));
				row.add(Convert.toStr(rescueListVo.getWordSecondsDesc(), "/"));
				row.add(Convert.toStr(rescueListVo.getRescueDesc()));
				row.add(Convert.toStr(rescueListVo.getIsHasUser() == 0 ? "未指派" : "已指派"));
				row.add(Convert.toStr(rescueListVo.getStatusName()));
				rows.add(row);
			}
			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			setResponseHeader(response, fileName);
			excelWriter = ExcelUtil.getBigWriter(rows.size());
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}
	}

	/**
	 * [业务解除-报障申请审批]——导出Excel
	 * @param userIdMap 即将解除的用户Id
	 */
	@ApiOperation("[业务解除-报障任务]——导出Excel")
	@ApiOperationSupport(order = 51)
	@ApiImplicitParam(name = "userId", value = "离职员工用户id", example = "1460777096537227266")
	@PostMapping("/reports/approve/download/excel")
	public void partsApplyListExcel(HttpServletResponse response,@RequestBody HashMap<String,Long> userIdMap) throws IOException {
		Long userId = userIdMap.get("userId");
		log.info("接收到的userId{}",userId);
		//获取报障 工单ids
		PlanApproveVO idVO = businessApproveService.malfunctionApproveStatistical(userId);
		List<Long> planIds = idVO.getPlanIds();

		//设置查询信息，查询相关数据
		PlanSearchDTOV2 planSearchDTOV2 = new PlanSearchDTOV2();
		planSearchDTOV2.setCurrent(1);
		planSearchDTOV2.setSize(Integer.MAX_VALUE);
		//目标结果
		List<PlanResultVo> records;
		if(Func.isNotEmpty(planIds)){
			planSearchDTOV2.setPlanIds(planIds);
			records = maintainPlanContentService.getV2Page(planSearchDTOV2).getRecords();
		} else {
			records = new ArrayList<>();
		}

		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<List<String>> rows = new ArrayList<>();
			List<String> title = CollUtil.newArrayList
				("任务编号", "执行人", "实际执行人", "报障状态", "电梯状态", "故障原因", "任务耗时/耗时", "组别", "项目", "梯号", "报障人", "渠道来源", "图片数量", "任务类型", "任务描述", "创建时间", "开始时间", "完成时间", "审核状态", "维修状态", "配件数量");
			rows.add(title);
			if (Func.isNotEmpty(records)) {
				for (PlanResultVo planListVo : records) {
					List<String> row = new ArrayList<>(16);
					row.add(planListVo.getId().toString());
					row.add(planListVo.getUserNames());
					row.add(Convert.toStr(planListVo.getRealUserNames(), "/"));
					row.add(planListVo.getIsStop() == 1 ? "正常" : "停梯");
					row.add(planListVo.getElevatorIsStop() == 1 ? "正常" : "停梯");
					row.add(Convert.toStr(planListVo.getProblemItemName()));
					row.add(planListVo.getWorkMinute() == null ? "/" : Convert.toStr(planListVo.getWorkMinute()));
					row.add(planListVo.getGroupName());
					row.add(planListVo.getBuildingName());
					row.add(planListVo.getElevatorAddress());
					row.add(planListVo.getFaultName());
					row.add(planListVo.getPlanFromName());
					row.add(planListVo.getImageNum().toString());
					row.add("维修");
					row.add(Convert.toStr(planListVo.getPlanCheckTypeName(), ""));
					row.add(DateUtil.format(planListVo.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
					row.add(DateUtil.format(planListVo.getBeginTime(), "yyyy-MM-dd HH:mm:ss"));
					row.add(DateUtil.format(planListVo.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
					row.add(planListVo.getApproveStatusName());
					row.add(planListVo.getStatusName());
					row.add(Convert.toStr(planListVo.getOwnPlanIds(), "0"));
					rows.add(row);
				}
			}
			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			setResponseHeader(response, fileName);
			excelWriter = ExcelUtil.getBigWriter(rows.size());
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}

	}

	/**
	 * [业务解除-配件审批]——导出Excel
	 * 复用 partApproveStatistical 方法，获取
	 * @param userIdMap 即将解除的用户Id
	 */
	@ApiOperation("[业务解除-配件任务]——导出Excel")
	@ApiOperationSupport(order = 61)
	@ApiImplicitParam(name = "userId", value = "离职员工用户id", example = "1460777096537227266")
	@PostMapping("parts/approve/download/excel")
	public void reportListExcel(HttpServletResponse response,@RequestBody HashMap<String,Long> userIdMap) throws IOException {
		Long userId = userIdMap.get("userId");
		log.info("接收到的userId{}",userId);

		//获取 plan id 列表
		PlanApproveVO idVO = maintainPlanContentService.partApproveStatistical(userId);
		List<Long> planIds = idVO.getPlanIds();
		//目标结果
		List<SparepartPricesVO> records = new ArrayList<>();
		if (Func.isNotEmpty(planIds)) {
			//遍历任务id 获取工单列表
			for (Long id : planIds) {
				//设置查询信息，查询相关数据
				SparepartPricesSearchDTO sparepartPricesSearchDTO = new SparepartPricesSearchDTO();
				sparepartPricesSearchDTO.setPlanId(id);
				//暴力条件
				sparepartPricesSearchDTO.setCurrent(1);
				sparepartPricesSearchDTO.setSize(Integer.MAX_VALUE);
				List<SparepartPricesVO> r = sparepartPricesService.planPage(sparepartPricesSearchDTO).getRecords();
				if (Func.isNotEmpty(r)) {
					records.addAll(r);
				}
			}
		}
		//导出操作
		ExcelWriter excelWriter = null;
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			List<List<String>> rows = new ArrayList<>();
			List<String> title = CollUtil.newArrayList
				("配件任务id", "项目名称", "电梯梯号", "关联任务ID", "关联任务类型", "申请人", "配件名称", "配件型号", "数量",
					"执行人", "实际执行人", "电梯状态", "申请时间", "配件进度", "审核状态");
			rows.add(title);
			if (Func.isNotEmpty(records)) {
				for (SparepartPricesVO s : records) {
					List<String> row = new ArrayList<>(16);
					row.add(Convert.toStr(s.getPlanId()));
					row.add(Convert.toStr(s.getBuildingName()));
					row.add(Convert.toStr(s.getElevatorAddress()));
					row.add(Convert.toStr(s.getParentPlanId()));
					row.add(Convert.toStr("维修"));
					row.add(Convert.toStr(s.getCreateUserName()));
					//配件名称处理
					String partName = s.getPartName();
					String[] split = partName.split(";");
					row.add(split[split.length-1]);
					row.add(Convert.toStr(s.getPartNO()));
					row.add(Convert.toStr(s.getPartNum()));
					row.add(Convert.toStr(s.getUserNames()));
					row.add(Convert.toStr(s.getSignUsers()));
					row.add(Convert.toStr(s.getIsStop() == 1 ? "正常" : "停梯"));
					row.add(Convert.toStr(s.getCreateTime()));
					row.add(Convert.toStr(s.getStatus() == 5 ? "待开始" : "更换中"));
					row.add(Convert.toStr("审核通过"));
					rows.add(row);
				}
			}
			String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx";
			setResponseHeader(response, fileName);
			excelWriter = ExcelUtil.getBigWriter(rows.size());
			excelWriter.write(rows, true);
			excelWriter.flush(outputStream, true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (excelWriter != null) {
				excelWriter.close();
			}
			outputStream.close();
		}
	}

	/**
	 * 功能描述: 发送响应流方法
	 */
	private void setResponseHeader(HttpServletResponse response, String fileName) {
		try {
			try {
				fileName = new String(fileName.getBytes(), "ISO8859-1");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			response.setContentType("application/octet-stream;charset=ISO8859-1");
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			response.addHeader("Pargam", "no-cache");
			response.addHeader("Cache-Control", "no-cache");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}


}
